4
4
.
.
1
1
.
.
6
6
V
V
i
i
d
d
e
e
o
o
P
P
l
l
a
a
y
y
e
e
r
r
(
(
A
A
V
V
K
K
i
i
t
t
-
-
n
n
o
o
d
d
e
e
f
f
a
a
u
u
l
l
t
t
c
c
o
o
n
n
t
t
r
r
o
o
l
l
s
s
)
)
I
I
n
n
f
f
o
o
Since SwiftUI doesn't support View for playing Video we will have to use the one from AVKit and put a wrapper around it.
Wrapper code will be placed inside PlayerView.swift File so that it doesn’t get in the way of usefull code.
This solution has no built in controls so we have to implement them ourselves.
Project: TestVdeo8
W
W
r
r
a
a
p
p
p
p
e
e
r
r
a
a
r
r
o
o
u
u
n
n
d
d
A
A
V
V
K
K
i
i
t
t
[
[
R
R
]
]
In this example we create PlayerView.swift File in which we create PlayerView asa wrappr around AVPlayer.
This will allow us to use PlayerView to pla Video while ignoring the wrapper code that was needed to make it work.
PlayerView.swift
import SwiftUI
import AVKit
import AVFoundation
//================================================================================
// STRUCT: PlayerView
//================================================================================
struct PlayerView: UIViewRepresentable {
let player : AVPlayer
func makeUIView(context: Context) -> UIView { return PlayerUIView(player: player) }
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) { }
}
//================================================================================
// STRUCT: PlayerUIView
//================================================================================
class PlayerUIView: UIView {
private let playerLayer = AVPlayerLayer()
init(player: AVPlayer) {
super.init(frame: .zero)
playerLayer.player = player
layer.addSublayer(playerLayer)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
}
V
V
i
i
d
d
e
e
o
o
f
f
r
r
o
o
m
m
U
U
R
R
L
L
In this example we play Video from URL.
ContentView.swift
import SwiftUI
import AVKit
//================================================================================
// STRUCT: ContentView
//================================================================================
struct ContentView: View {
var player = AVPlayer(url: URL(string: "https://ivoronline.com/BachataBasic.mp4")!)
var body: some View {
PlayerView(player: player)
.onAppear { self.player.play() }
}
}
V
V
i
i
d
d
e
e
o
o
f
f
r
r
o
o
m
m
l
l
o
o
c
c
a
a
l
l
F
F
i
i
l
l
e
e
In this example we play Video from local File.
ContentView.swift
import SwiftUI
import AVKit
//================================================================================
// STRUCT: ContentView
//================================================================================
struct ContentView: View {
var player = AVPlayer(url: Bundle.main.url(forResource: "Salsa - Sako", withExtension: "mp4")!)
var body: some View {
PlayerView(player: player)
.onAppear { self.player.play() }
}
}
C
C
o
o
n
n
t
t
r
r
o
o
l
l
s
s
P
P
l
l
a
a
y
y
/
/
P
P
a
a
u
u
s
s
e
e
We will now add ControlsView which will contain a Button with which we can Play / Pause Video.
ContentView.swift
import SwiftUI
import AVKit
import AVFoundation
//================================================================================
// STRUCT: ContentView
//================================================================================
struct ContentView: View {
var player = AVPlayer(url: URL(string: "https://ivoronline.com/BachataBasic.mp4")!)
var body: some View {
VStack {
PlayerView (player: player)
ControlsView(player: player)
}
}
}
//================================================================================
// STRUCT: ControlsView
//================================================================================
struct ControlsView : View {
@State var play = false
let player : AVPlayer
var body: some View {
Button(play ? "pause" : "play") {
self.play.toggle()
switch(self.play) {
case true : self.player.play()
case false : self.player.pause()
}
}
}
}
S
S
l
l
i
i
d
d
e
e
r
r
m
m
o
o
v
v
e
e
s
s
V
V
i
i
d
d
e
e
o
o
By moving slider you can choose specific frame.
ContentView.swift
import SwiftUI
import AVKit
import AVFoundation
//================================================================================
// STRUCT: ContentView
//================================================================================
struct ContentView: View {
var player = AVPlayer(url: URL(string: "https://ivoronline.com/BachataBasic.mp4")!)
var body: some View {
VStack {
PlayerView (player: player)
ControlsView(player: player)
}
}
}
//================================================================================
// STRUCT: ControlsView
//================================================================================
struct ControlsView : View {
@State var play = false
@State var seekPos = 0.0
let player : AVPlayer
var body: some View {
VStack {
Button(play ? "pause" : "play") {
self.play.toggle()
switch(self.play) {
case true : self.player.play()
case false : self.player.pause()
}
}
Slider(value: $seekPos, in: 0...1, step: 0.01, onEditingChanged: { _ in
guard let item = self.player.currentItem else { return }
let targetTime = self.seekPos * item.duration.seconds
self.player.seek(to: CMTime(seconds: targetTime, preferredTimescale: 600))
})
}
}
}